/*******************************************************************************
* CSCI321 Major Project 2010 - University of Wollongong                        *
********************************************************************************
* Program:		WeaveEdit                                              *
* Authors:		Simon Allen (Student#: 3457527, Unix: sa655)           *
*			Scott Bellis (Student#: 3479110, Unix: sjb900)         *
*			Jason Benger (Student#: 3454137, Unix: jb960)          *
*			Timothy Newmarch (Student#: 3459263 Unix: tgn336)      *
*			Benjamin Richardson (Student# 3467752, Unix: bjr401)   *
* Supervisor:		Daniel F. Saffioti                                     *
* Modified:		28/10/2010                                             *
* Description:		WeaveEdit is a highly portable collaborative text      *
*			editor and word processor, allowing several users to   *
*			read and edit multiple documents at the same time.     *
*			It is available for Mac OS X, Windows and Linux,       *
*			utilising Nokia's Qt development framework.            *
*******************************************************************************/


#include "constants.h"
#include "MainWindow/mainwindow.h"

#include <QtGui/QApplication>

#include <iostream>
#include <cstdlib>

using namespace std;


static bool processCmdArgs(int argc, char *argv[]);
static void printHelp();
static void printVersion();

int main(int argc, char *argv[])
{
	// Run commands line utility arguements and quit.
	if (processCmdArgs(argc, argv))
		return 0;

	/*  Disable Avahi warnings on Linux systems because we are using the
	  * Avahi compatibility layer which is incldued in most distros, not Bonjour
	  */
#ifdef linux
	setenv("AVAHI_COMPAT_NOWARN", "1", 1);
#endif

	QApplication weaveEdit(argc, argv);

	MainWindow *window = MainWindow::getInstance();
	window->setExecutableName(QString(argv[0]));	//save the name of the program for use when forking processes

	window->show();

	if (argc < 2)
		window->showWelcome();	//if no cmd line stuff passed

	return weaveEdit.exec(); // Begin GUI event loop.
}

/**
  * Process arguements passed on command line.
  */
bool processCmdArgs(int argc, char *argv[])
{
	bool isArg = false;

	for (int i = 1; i < argc; i++)
	{
		if (QString(argv[i]) == CMD_ARG_HELP)
		{
			printHelp();

			isArg = true;
		}

		else if (QString(argv[i]) == CMD_ARG_VERSION)
		{
			printVersion();

			isArg = true;
		}
	}

	return isArg;
}

/**
  * Print basic command line help information. More switches may be added if need be.
  */
void printHelp()
{
	cout << endl << PROGRAM_NAME << " usage:\n\t--help\t\tPrint this help information."
					"\n\t--version\tPrint version information.\n\n";
}

/**
  * Print program version ala unix standards.
  */
void printVersion()
{
	cout << endl << PROGRAM_NAME << " version " << PROGRAM_VERSION << endl << endl;
}
